import axios from 'axios'
import { ElMessage } from 'element-plus';

const request = axios.create({
	withCredentials: true,
	baseURL: "/",
	timeout: 10000
})

request.interceptors.request.use(
	config => {
		// 设置请求头格式 这里是表单 你也可以改成json
		config.headers['Content-Type'] = 'application/json;charset=UTF-8';
		// 去除重复的data参数
		for (const k in config.params) {
			if (config.data && config.data[k]) {
				delete config.data[k];
			}
		}
		return config;
	},
	error => {
		ElMessage.error(error)
		return Promise.reject(error)
	}
)

request.interceptors.response.use(
	response => {
		const res = response.data
		if (!res.status) {
			return Promise.reject(new Error(res.message || 'Error'))
		} else {
			return res.data;
		}
	},
	error => {
		if(error.response && error.response.data && error.response.data.code == 401){
			// removeToken()
		}
		return Promise.reject(error)
	}
)

export default request
